﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint.Administration;

namespace PowerShell.SPUtility
{
    public class SPULS : SPDiagnosticsServiceBase
    {
        /// <summary>
        // Help from here: http://geekswithblogs.net/venkatx5/archive/2010/12/09/how-to-use-uls-in-sharepoint-2010-for-custom-code.aspx
        // Logging service to SharePoint ULS for PowerShell jobs run against SharePoint
        // Next step is to make this a PowerShell Module
        //http://blogs.msdn.com/b/saveenr/archive/2010/03/08/how-to-create-a-powershell-2-0-module-and-cmdlet-with-visual-studio-2010-screencast-included.aspx

        /// </summary>
        public static string ProductDiagnosticAreaName = "PowerShell";
        public static string CategoryName = "PowerShell Log";
        public static uint uintEventID = 700;
        private static SPULS _Current;
        public static SPULS Current
        {
            get
            {
                if (_Current == null)
                {
                    _Current = new SPULS();
                }

                return _Current;
            }
        }

        private SPULS()
            : base("PowerShell", SPFarm.Local)
        {

        }

        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
            List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>
            {
                new SPDiagnosticsArea(ProductDiagnosticAreaName, new List<SPDiagnosticsCategory>
                {
                    new SPDiagnosticsCategory(CategoryName, TraceSeverity.Medium, EventSeverity.Error)
                })
            };

            return areas;
        }

        public static string LogError(string errorMessage)
        {
            string strExecutionResult = "Message Not Logged in ULS.";
            try
            {
                SPDiagnosticsCategory category = SPULS.Current.Areas[ProductDiagnosticAreaName].Categories[CategoryName];
                SPULS.Current.WriteTrace(uintEventID, category, TraceSeverity.Unexpected, errorMessage);
                strExecutionResult = "Message Logged";
            }
            catch (Exception ex)
            {
                strExecutionResult += ex.Message;
            }

            return strExecutionResult;

        }

        public static string LogVerbose(string errorMessage)
        {
            string strExecutionResult = "Message Not Logged in ULS.";
            try
            {
                SPDiagnosticsCategory category = SPULS.Current.Areas[ProductDiagnosticAreaName].Categories[CategoryName];
                SPULS.Current.WriteTrace(uintEventID, category, TraceSeverity.Verbose, errorMessage);
                strExecutionResult = "Message Logged";
            }
            catch (Exception ex)
            {
                strExecutionResult += ex.Message;
            }

            return strExecutionResult;

        }

    }
}
